﻿using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using MovieMadness.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations;

namespace MovieMadness.Data
{
    public static class Seeder
    {
        public static void Seed(ApplicationDbContext db, bool roles = true, bool users = true, bool movies = true, bool actors = true, bool reviews = true)
        {
            if(roles) SeedRoles(db);
            if (users) SeedUsers(db);
            if (movies) SeedMovies(db);
            if (actors) SeedActors(db);
            if (reviews) SeedReviews(db);
        }

        private static void SeedRoles(ApplicationDbContext db)
        {
            var store = new RoleStore<IdentityRole>(db);
            var manager = new RoleManager<IdentityRole>(store);

            manager.Create(new IdentityRole()
            {
                Name = "User"
            });

            manager.Create(new IdentityRole() { Name = "Admin" });
        }

        private static void SeedUsers(ApplicationDbContext db) 
        {
            var store = new UserStore<ApplicationUser>(db);
            var manager = new UserManager<ApplicationUser>(store);

            if (!db.Users.Any(x => x.UserName == "test@test.com")) 
            {
                ApplicationUser user = new ApplicationUser()
                {
                    UserName = "test@test.com",
                    Email = "test@test.com"
                };
                manager.Create(user, "123123");
                manager.AddToRole(user.Id, "User");
            }
            if(!db.Users.Any(x => x.UserName == "admin@test.com"))
            {
                ApplicationUser user = new ApplicationUser()
                {
                    UserName = "admin@test.com",
                    Email = "admin@test.com"
                };
                manager.Create(user, "123123");
                manager.AddToRole(user.Id, "Admin");
            }
            }

            private static void SeedMovies(ApplicationDbContext db)
            {
                string testId = db.Users.FirstOrDefault( x => x.UserName == "test@test.com").Id;
                string adminId = db.Users.FirstOrDefault( x => x.UserName == "admin@test.com").Id;

                db.Movies.AddOrUpdate(x => x.MovieId,
                    new Movie() { MovieId = 1, Title = "Movie1", Year = 2015, ActorId = 1, Genre = Genre.Action, Rating = Rating.G },
                    new Movie() { MovieId = 2, Title = "Movie2", Year = 2014, ActorId = 2, Genre = Genre.Comedy, Rating = Rating.PG },
                    new Movie() { MovieId = 3, Title = "Movie3", Year = 2013, ActorId = 3, Genre = Genre.Romance, Rating = Rating.PG13 },
                    new Movie() { MovieId = 4, Title = "Movie4", Year = 2012, ActorId = 4, Genre = Genre.Horror, Rating = Rating.R },
                    new Movie() { MovieId = 5, Title = "Movie5", Year = 2011, ActorId = 5, Genre = Genre.Documentary, Rating = Rating.PG13 }
                );
              
            }
        
            private static void SeedReviews(ApplicationDbContext db)
            {
 	             db.Reviews.AddOrUpdate(x => x.ReviewId,
                    new Review() { ReviewId = 1, Post = "Review1", DatePosted = DateTime.Now, MovieId = 1, Rating = 1},
                    new Review() { ReviewId = 2, Post = "Review2", DatePosted = DateTime.Now, MovieId = 2, Rating = 2},
                    new Review() { ReviewId = 3, Post = "Review3", DatePosted = DateTime.Now, MovieId = 3, Rating = 3},
                    new Review() { ReviewId = 4, Post = "Review4", DatePosted = DateTime.Now, MovieId = 4, Rating = 4},
                    new Review() { ReviewId = 5, Post = "Review5", DatePosted = DateTime.Now, MovieId = 5, Rating = 5}
                 );
            }

            private static void SeedActors(ApplicationDbContext db)
            {
 	             db.Actors.AddOrUpdate(x=>x.ActorId,    
                     new Actor() { ActorId = 1, MovieId = 1, Name = "Actor1" },
                     new Actor() { ActorId = 2, MovieId = 2, Name = "Actor2" },
                     new Actor() { ActorId = 3, MovieId = 3, Name = "Actor3" },
                     new Actor() { ActorId = 4, MovieId = 4, Name = "Actor4" },
                     new Actor() { ActorId = 5, MovieId = 5, Name = "Actor5" }
                 );
            }

        
        }
}

